package com.amqr.testfragment.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.amqr.testfragment.R;
import com.amqr.testfragment.fragment.style1.FourFragment;
import com.amqr.testfragment.fragment.style1.OneFragment;
import com.amqr.testfragment.fragment.style1.ThreeFragment;
import com.amqr.testfragment.fragment.style1.TwoFragment;

/**
 * User: LJM
 * Date&Time: 2016-11-23 & 22:01
 * Describe: Describe Text
 *
 * 整体思路,一进入Activity只add进入头两个Fragment,而且hide第二个,展示第一个
 * 以后如果切换到任意的Fragment,那么先判断事务里面有没有这个Fragment,
 * 无则加之,然后隐藏上一个,展示当前新切换到的
 * 有则隐藏上一个,展示当前的新切换到的
 */
public class StyleSecondActivity extends AppCompatActivity {

    private Button[] mTabs;

    // ======== 四个TAB
    private OneFragment sOneFragment;
    private TwoFragment sTwoFragment;
    private ThreeFragment sThreeFragment;
    private FourFragment sFourFragment;


    private Fragment[] fragments;
    private int index;
    private int currentTabIndex;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_style_second);

        mTabs = new Button[4];
        mTabs[0] = (Button) findViewById(R.id.mBtOneFragment);
        mTabs[1] = (Button) findViewById(R.id.mBtTwoFragment);
        mTabs[2] = (Button) findViewById(R.id.mBtThreeFragment);
        mTabs[3] = (Button) findViewById(R.id.mBtFourFragment);



        // select first tab
        mTabs[0].setSelected(true);

        // 实例化Tab
        sOneFragment = new OneFragment();
        sTwoFragment = new TwoFragment();
        sThreeFragment = new ThreeFragment();
        sFourFragment = new FourFragment();

        fragments = new Fragment[]{sOneFragment, sTwoFragment,  sThreeFragment, sFourFragment};

        // 一开始只把 第一个 和 第二个 Fragment添加到事务里面,(方便切换时有数据)
        // 然后默认展示第一个,第二个隐藏起来
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, sOneFragment)
                .add(R.id.fragment_container, sTwoFragment).hide(sTwoFragment).show(sOneFragment)
                .commit();
    }

    /**
     * 每一个按钮都带有android:onClick="onTabClicked"
     * 不采用常规的复写onClick而采用这种方式,是为了统一操作这些按钮
     * @param view
     */
    public void onTabClicked(View view) {
        switch (view.getId()) {
            case R.id.mBtOneFragment:
                index = 0;
                break;
            case R.id.mBtTwoFragment:
                index = 1;
                break;

            case R.id.mBtThreeFragment:
                index = 2;
                break;
            case R.id.mBtFourFragment:
                index = 3;
                break;
        }
        if (currentTabIndex != index) { // 切换了Fragment
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();

            trx.hide(fragments[currentTabIndex]); // 隐藏原来的
            if (!fragments[index].isAdded()) { // 如果当前的没有被添加到FragmentTransaction,添加之
                trx.add(R.id.fragment_container, fragments[index]);
            }
            trx.show(fragments[index]).commit(); // 添加事务,切换Fragment
        }
        mTabs[currentTabIndex].setSelected(false);
        mTabs[index].setSelected(true);
        currentTabIndex = index;
    }

}
